//package com.ly.mpayment.rest.system.slow;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alipay.api.AlipayApiException;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.internal.util.AlipaySignature;
//import com.alipay.api.internal.util.WebUtils;
//import com.alipay.api.request.AlipayTradePrecreateRequest;
//import com.alipay.api.request.AlipayTradeWapPayRequest;
//import com.alipay.api.response.AlipayTradePrecreateResponse;
//import com.alipay.api.response.AlipayTradeWapPayResponse;
//import com.ly.mpayment.core.util.DateUtils;
//import com.ly.mpayment.core.util.StringUtils;
//import com.ly.mpayment.persist.entity.slow.SlowOrder;
//import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
//import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
//import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
//import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
//import com.wechat.pay.contrib.apache.httpclient.util.RsaCryptoUtil;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.utils.URIBuilder;
//import org.apache.http.util.EntityUtils;
//import org.apache.log4j.Logger;
//import org.springframework.stereotype.Component;
//
//import javax.crypto.BadPaddingException;
//import javax.servlet.http.HttpServletRequest;
//import javax.ws.rs.POST;
//import javax.ws.rs.Path;
//import javax.ws.rs.core.Context;
//import java.io.*;
//import java.math.BigDecimal;
//import java.net.URI;
//import java.net.URISyntaxException;
//import java.security.KeyFactory;
//import java.security.NoSuchAlgorithmException;
//import java.security.PrivateKey;
//import java.security.Signature;
//import java.security.cert.*;
//import java.security.spec.InvalidKeySpecException;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.util.Base64;
//import java.util.Random;
//
//@Component
//@Path("system/alipay")
//public class AliPayRest {
//    Logger logger = Logger.getLogger(AliPayRest.class);
//    /**
//     * 支付宝回调
//     * @param data
//     * @param request
//     * @return
//     */
//    @POST
//    @Path("call_back")
//    public String callBack(String data,@Context HttpServletRequest request) {
//        logger.info("alipay_back:"+data);
//
//        //AlipaySignature.rsaSign();
//        String signature = request.getHeader("Wechatpay-Signature");
//        String timestamp = request.getHeader("Wechatpay-Timestamp");
//        String nonce = request.getHeader("Wechatpay-Nonce");
//        String serial = request.getHeader("Wechatpay-Serial");
//        String authorization = request.getHeader("Authorization");
//
//        logger.info("wechat_callBack:"+data+",sign:"+signature+",serial:"+serial+",nonce:"+nonce+",timestamp:"+timestamp+",authorization:"+authorization);
//        if(StringUtils.isBlank(data)){
//            return "0";
//        }
//
//        JSONObject wechat = JSONObject.parseObject(data);
//
//
//        return "success";
//    }
//
//
//    //请求
//    HttpClient httpClient ;
//
//    private  String appid = "2021002189645189";
//    //回调通知地址
//    private  String notifyUrl = "https://wx.kkshs.com/mpayment/client/alipay/call_back";
//    //下单地址
//    private String payH5 = "https://openapi.alipay.com/gateway.do";
//
//    final static String CHARSET = "utf-8";
//    /**
//     * APP_PRIVATE_KEY	开发者私钥，由开发者自己生成。
//     */
//    private  String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCG2Ku5ME2djKIpStHaRYTdCLmBWSmV/Q5u2N8fiLEC1KFCVpgWKDOgINnZ8AldGi+3mNDuX6woMh7VhvP0GOpT+djD6pGnv1Jdy8UOsnN1AXm9lYMy2m/y5YRlFSd5cQavu1baQaR/5S2natLzd6e6Ajyegxlaq7OGC5nDi6da8bAg18bdJ9ZxunXC+p8rzpPGmaXnTz8PNtWCpGCH2d4ewb4O5706zZCJ68urqdACFRxLh7AHPp997Y+laFNdCnriCesnY2dv8icVrRKX5buSpnVMyPLdSnJu8ukRoSftgDveHawqywnWPfqSTn/M2Oxr7O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bOaSfY+ZBAoGAEA/lxuIMBJ3pwXlfhxkoedxvXFV410St5WXSt1vB11x2XGXL+lCfL8xDsLzwL2WvokjSNKMC/n47myQ/YNUAXdIfNPN1OaDT/mwa1R8LiT4dWRKGxTejKd5Nkq/dNz8Wr2HI8kOEBXAVFaGb0VeoMU05eo70hucj0mGet9bWmZQ=";
//
//    /**
//     * 支付宝公钥，由支付宝生成。
//     */
//    private  String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhtiruTBNnYyiKUrR2kWE3Qi5gVkplf0ObtjfH4ixAtShQlaYFigzoCDZ2fAJXRovt5jQ7l+sKDIe1Ybz9BjqU/nYw+qRp79SXcvFDrJzdQF5vZWDMtpv8uWEZRUneXEGr7tW2kGkf+Utp2rS83enugI8noMZWquzhguZw4unWvGwINfG3SfWcbp1wvqfK86Txpml508/DzbVgqRgh9neHsG+Due9Os2QievLq6nQAhUcS4ewBz6ffe2PpWhTXQp64gnrJ2Nnb/InFa0Sl+W7kqZ1TMjy3UpybvLpEaEn7YA73h2sKssJ1j36kk5/zNjsa+zo9608KhWpdY/iqPpEjQIDAQAB";
//
//    private String timeoutExpress = "5m";
//
//    private String quit_url = "https://wx.kkshs.com/mobile/index.html";
//
//    /**
//     * 返回h5支付地址
//     * @return
//     */
//    public AlipayTradeWapPayResponse payTradePrecreate(SlowOrder slowOrder,String title){
//        AlipayClient alipayClient = new DefaultAlipayClient( payH5,appid,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,"RSA2");  //获得初始化的AlipayClient
//        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); //创建API对应的request类
//        WebUtils.setNeedCheckServerTrusted(false);
//        request.setNotifyUrl(notifyUrl);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("out_trade_no",slowOrder.getOutTradeNo());
//        jsonObject.put("total_amount",slowOrder.getPayPrice().setScale(2,BigDecimal.ROUND_HALF_DOWN));
//        jsonObject.put("subject", title);
//        //订单允许的最晚付款时间
//        jsonObject.put("timeout_express",timeoutExpress);
//        jsonObject.put("product_code","QUICK_WAP_WAY");
//        jsonObject.put("quit_url",quit_url);
//        request.setBizContent(jsonObject.toString());
//        try {
//            logger.info("payTradePrecreate:"+jsonObject.toString());
//            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
//            logger.info("payTradePrecreate:"+JSONObject.toJSONString(response));
//            if(response.isSuccess()){
//                return response;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.info("AlipayTradePrecreateResponse:{}"+e.getMessage());
//        }
//        return null;
//    }
//
//    public static  void main(String[] args) throws Exception {
//        SlowOrder slowOrder = new SlowOrder();
//        slowOrder.setPayPrice(new BigDecimal(1));
//        slowOrder.setOutTradeNo("1454534423432423423");
//        new AliPayRest().payTradePrecreate(slowOrder,"移动");
//        // 使用商户私钥解密
//
//    }
//
//
//
//    /**
//     * 生成支付订单号
//     *
//     * @return
//     */
//    public  String wxOrderNo() {
//        // 订单前缀
//        String temp = new DateUtils().toTimeStamp();
//        // 生成15位随机序列
//        String randomSte = getRandomString(15);
//        return temp + randomSte;
//    }
//
//    private  String getRandomString(int length) { // length表示生成字符串的长度
//        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
//        Random random = new Random();
//        StringBuffer sb = new StringBuffer();
//        for (int i = 0; i < length; i++) {
//            int number = random.nextInt(base.length());
//            sb.append(base.charAt(number));
//        }
//        return sb.toString().toUpperCase();
//    }
//
//}
